/**
 * \file logFile.cpp
 * \brief logFile class implementation
 * \author Marc Laval
 * \version 0.1
 * \date 10/2011
 *
 */

#include "logfile.h"

std::string LogFile::type_names = {"Info", "Warning", "Error"};
LogFile* LogFile::m_log = 0;

//-------------------------------------------------------------------------------
/*! \brief Instanciate a log file to save informations (errors, warning etc)
 *  \param filename Filename as string
 *
 * LogFile is a singleton classe, usable by any object in the application.
 * The goal is to make it accessible from anywhere in the application
 * to be able to record any usable information for debug.
 */
LogFile::LogFile(std::string filename) : outputfile(filename.c_str(), ios::out | ios::trunc)
{
    if(outputfile.fail())
    {
        throw(Error("Unable to open output log file "+filename));
    }

}

//-------------------------------------------------------------------------------
/*! \brief Standard destructor
 *   Close the file
 */
LogFile::~LogFile()
{
    outputfile.close();
}

//-------------------------------------------------------------------------------
/*! \brief Static method to get the instance of LogFile class
 *  \return Pointer to the instance
 *  If there is no instance of the class, a new one is created.
 */
LogFile * LogFile::getInstance()
{
    if(m_log == 0)
    {
        m_log = new LogFile(LOGFILE_NAME);
    }

    return m_log;
}

//-------------------------------------------------------------------------------
/*! \brief Save a message in the log file
 *  \param lt Type of the message
 *  \param msg String containing the message
 */
void LogFile::log(LogType lt, std::string msg)
{
    outputfile << "[" << type_names[lt] << "] " << msg << endl;
}
